package com.ruoyi.project.wx.controller;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.wx.domain.CmsContentDO;
import com.ruoyi.project.wx.service.CmsContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 后台设置Controller
 *
 * @author long
 * @date 2021-04-15
 */
@RestController
@RequestMapping("/wx/wxCmsContent")
public class CmsContentController extends BaseController
{
	@Autowired
	private CmsContentService cmsContentService;

	/**
	 * 查询后台设置列表
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('wx:wxCmsContent:list')")
	@GetMapping("/list")
	public TableDataInfo list(CmsContentDO cmsContent)
	{
		startPage();
		Map<String, Object> stringObjectMap = BeanUtil.beanToMap(cmsContent);
		stringObjectMap.put("delFlag","0");
		stringObjectMap.put("sort","update_at");
		stringObjectMap.put("order","desc");
		List<CmsContentDO> list = cmsContentService.list(stringObjectMap);
		return getDataTable(list);
	}

	/**
	 * 导出后台设置列表
	 */
	@PreAuthorize("@ss.hasPermi('wx:wxCmsContent:export')")
	@Log(title = "后台设置", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult export(CmsContentDO cmsContent)
	{
		Map<String, Object> stringObjectMap = BeanUtil.beanToMap(cmsContent);
		List<CmsContentDO> list = cmsContentService.list(stringObjectMap);
		ExcelUtil<CmsContentDO> util = new ExcelUtil<CmsContentDO>(CmsContentDO.class);
		return util.exportExcel(list, "wxCmsContent");
	}

	/**
	 * 获取后台设置详细信息
	 */
	@PreAuthorize("@ss.hasPermi('wx:wxCmsContent:query')")
	@GetMapping(value = "/{id}")
	public AjaxResult getInfo(@PathVariable("id") Long id)
	{
		return AjaxResult.success(cmsContentService.get(id));
	}

	/**
	 * 新增后台设置
	 */
	@PreAuthorize("@ss.hasPermi('wx:wxCmsContent:add')")
	@Log(title = "后台设置", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody CmsContentDO cmsContent)
	{

		cmsContent.setUpdateAt(new Date());
		return toAjax(cmsContentService.save(cmsContent));
	}

	/**
	 * 修改后台设置
	 */
	@PreAuthorize("@ss.hasPermi('wx:wxCmsContent:edit')")
	@Log(title = "后台设置", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody CmsContentDO cmsContent)
	{
		cmsContent.setUpdateAt(new Date());
		return toAjax(cmsContentService.update(cmsContent));
	}

	/**
	 * 删除后台设置
	 */
	@PreAuthorize("@ss.hasPermi('wx:wxCmsContent:remove')")
	@Log(title = "后台设置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult remove(@PathVariable Long[] ids)
	{
		if(ids.length>0){
			for (Long id : ids) {
				CmsContentDO cmsContent = cmsContentService.get(id);
				cmsContent.setUpdateAt(new Date());

				cmsContentService.update(cmsContent);
			}
		}
		return toAjax(ids.length);
		//return toAjax(cmsContentService.deleteCmsContentByIds(ids));
	}
}
